﻿using System;
using CN2P.ServiceModel.ServiceClients;

namespace CN2P.Tests
{
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class UserSessionServiceTest : BaseServiceTest
    {
        private const string SESSION_ITEM = @"{
""userId"":""aaa@aaa.com"",
""prodId"":""1234""
}";

        [TestMethod]
        [TestCategory("Session")]
        public void AddViewedItems()
        {
            this.Service.SessionService.DeleteAll();

            var userA = GetRandomUserId();
            var userB = GetRandomUserId();
            this.Service.SessionService.Post(userA, SESSION_ITEM.Replace("aaa@aaa.com", userA).Replace("1234", "111"));
            this.Service.SessionService.Post(userA, SESSION_ITEM.Replace("aaa@aaa.com", userB).Replace("1234", "121"));
            this.Service.SessionService.Post(userA, SESSION_ITEM.Replace("aaa@aaa.com", userA).Replace("1234", "131"));
            this.Service.SessionService.Post(userA, SESSION_ITEM.Replace("aaa@aaa.com", userB).Replace("1234", "141"));

            var res = this.Service.SessionService.Get(userA, 2);
            Assert.AreEqual(2, res.Count);

            Assert.AreEqual("111", res[0]);
            Assert.AreEqual("131", res[1]);
        }

        [TestMethod]
        [TestCategory("Session")]
        public void DeleteAll()
        {
            var userA = GetRandomUserId();
            var userB = GetRandomUserId();
            this.Service.SessionService.Post(userA, SESSION_ITEM.Replace("aaa@aaa.com", userA).Replace("1234", "111"));
            this.Service.SessionService.Post(userA, SESSION_ITEM.Replace("aaa@aaa.com", userB).Replace("1234", "121"));

            var res = this.Service.SessionService.Get(userA, 1);
            Assert.AreEqual(res.Count(), 1);

            this.Service.SessionService.DeleteAll();

            try
            {
                Service.SessionService.GetForUnknownUser(userA, 1);
                //Assert.Fail("Expected exception not found after user sessions have been deleted");
            }
            catch (UnexpectedServiceResponseException ex)
            {
                
            }
        }

        //[TestMethod]
        //public void PostViewedItemTest()
        //{
        //    var userId = GetRandomUserId();
        //    string postItem = SESSION_ITEM.Replace("aaa@aaa.com", userId);
        //    this.Service.SessionService.Post(userId, postItem);

        //    var res = this.Service.SessionService.Get(userId, 2);
        //    Assert.AreEqual(1, res.Count);

        //    Assert.AreEqual(1234, res[0].ProdId);
        //}
    }
}
